package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-08-01 11:10
 **/
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    private static final String CART = "cart_";

    /**
     * @param skuId
     * @param num
     * @param username
     * @return void
     * @description: 添加商品到购物车
     * @author 大佬味的小男孩
     * @date 2020/08/06 15:38
     */
    @Override
    public void addcart(String skuId, Integer num, String username) {
        //1. 查询Redis中是否有购物车商品
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
        if (orderItem != null) {
            // 2. 如果在Redis中存在该商品，则更新数量和价格和支付价格
            orderItem.setNum(orderItem.getNum() + num);
            if (orderItem.getNum() <= 0) {
                //删除商品
                redisTemplate.boundHashOps(CART + username).delete(skuId);
                return;
            }
            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
        } else {
            //3. 如果在Redis不存在商品
            // 3.1 根据sku的id查询sku的数据
            Result<Sku> skuResult = skuFeign.findById(skuId);
            Sku sku = skuResult.getData();
            // 3.2 根据spu的id查询spu的数据
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();
            //3.3 封装orderItem
            orderItem = this.Sku2OrderItem(sku, spu, num);
        }
        //4 添加到Redis
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
    }

    //查询购物车数据
    @Override
    public Map list(String username) {
        Map<Object, Object> map = new HashMap<>();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList", orderItemList);
        //购物车中商品总价格和总数量
        Integer num = 0;
        Integer money = 0;
        for (OrderItem orderItem : orderItemList) {
            num += orderItem.getNum();
            money += orderItem.getMoney();
        }
        map.put("totalNum", num);
        map.put("totalMoney", money);
        return map;
    }


    private OrderItem Sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        //spuid
        orderItem.setSpuId(sku.getSpuId());
        //skuid
        orderItem.setSkuId(sku.getId());
        //skuname
        orderItem.setName(sku.getName());
        //价格
        orderItem.setPrice(sku.getPrice());
        //数量
        orderItem.setNum(num);
        //总价格
        orderItem.setMoney(orderItem.getPrice() * num);
        //支付价格
        orderItem.setPayMoney(orderItem.getPrice() * num);
        //sku图片信息
        orderItem.setImage(sku.getImage());
        //总重量
        orderItem.setWeight(sku.getWeight() * num);
        //分类信息
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());


        return orderItem;
    }


}
